<?php
global $_GPC, $_W;
load()->library('qrcode');
class code{
    //生成二维码
//参数: $content:二维码的内容 string
//      $size:二维码的尺寸  string
    public function createCode($content,$size,$filename){
        $errorCorrectionLevel = "L";//错误校正级别
        $code = QRcode::png($content, $filename, $errorCorrectionLevel,$size);
        return $code;
    }
    //下载二维码
    //参数：二维码的内容数据content 大小size
    public function downloadCode($content,$size){
        $code = $this->createCode($content,$size);
        $name = random(8);
        header('cache-control:private');
        header('content-type:image/jpeg');
        header('content-disposition: attachment;filename="'.$name.'.jpg"');
        readfile($code);
    }
}
$code = new code();

$GLOBALS['frames'] = $this->getMainMenu2();
if($_GPC['id']){
setcookie("storeid",$_GPC['id']);
$cur_store = $this->getStoreById($_GPC['id']);  
$storeid=$_GPC['id'];
}else{
$storeid=$_COOKIE["storeid"];
$cur_store = $this->getStoreById($storeid); 
}
$uniacid=$_W['uniacid'];
//今日营业额
$time=strtotime(date("Y-m-d",time()));
//预定支付
$sql = "select IFNULL(sum(yd_price),0) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and create_time >= {$time} and status = 1";
$yd_price = pdo_fetchcolumn($sql);
//预定支付订单数
$sql_a = "select count(id) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and create_time >= {$time} and status = 1";
$yd_num = pdo_fetchcolumn($sql_a);
//消费支付
$sql_1 = "select IFNULL(sum(xf_price),0) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and com_time >= {$time} and status = 2";
$xf_price = pdo_fetchcolumn($sql_1)?pdo_fetchcolumn($sql_1):'0.00';
//消费支付订单数
$sql_1_a = "select count(id) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and com_time >= {$time} and status = 2";
$xf_num = pdo_fetchcolumn($sql_1_a)?pdo_fetchcolumn($sql_1_a):0;
//var_dump($xf_price);die;
//今日总营业额
$price = $yd_price + $xf_price;
//今天总订单数
$num = $yd_num + $xf_num;
//总预定支付
$sql_2 = "select IFNULL(sum(yd_price),0) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and status in(1,2)";
$yd_total_price = pdo_fetchcolumn($sql_2);
//总消费支付
$sql_2_a = "select IFNULL(sum(xf_price),0) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} 
and status = 2";
$xf_total_price = pdo_fetchcolumn($sql_2_a);
//总收入
$total_price = $xf_total_price+$yd_total_price;
//今天有效订单
$sql_3 = "select count(id) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} and 
com_time >= {$time} and status = 2";
$yx_num = pdo_fetchcolumn($sql_3)?pdo_fetchcolumn($sql_3):0;
//今天无效订单
$sql_4 = "select count(id) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} and 
com_time >= {$time} and status = 3";
$wx_num = pdo_fetchcolumn($sql_4)?pdo_fetchcolumn($sql_4):0;
//有效订单总数
$sql_5 = "select count(id) from ".tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$storeid} and 
status = 2";
$yx_total_num = pdo_fetchcolumn($sql_5)?pdo_fetchcolumn($sql_5):0;
//var_dump($yx_total_num);die;
//二维码
$qrcode = pdo_fetchcolumn('select qrcode from '.tablename('cjdc_store')." where uniacid = {$uniacid} and id = {$storeid}");
function  getCoade($storeid){
    function getaccess_token(){
      global $_W, $_GPC;
         $res=pdo_get('cjdc_system',array('uniacid' => $_W['uniacid']));
         $appid=$res['appid'];
         $secret=$res['appsecret'];
         
       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
       $data = curl_exec($ch);
       curl_close($ch);
       $data = json_decode($data,true);
       return $data['access_token'];
    }
     function set_msg($storeid){
       $access_token = getaccess_token();
        $data2=array(
        "scene"=>$storeid,
        "page"=>"zh_cjdianc/pages/seller/index",
        "width"=>100
               );
    $data2 = json_encode($data2);
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token."";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
         }
        $img=set_msg($storeid);
        $img=base64_encode($img);
  return $img;
  }
  //查询商家是否存在二维码
//var_dump($qrcode);die;
if(empty($qrcode)){
    $url =  time() . '.png';
    $url_a = '../addons/zh_cjdianc/img/'.$url;
    $code->createCode($storeid, "6",$url_a);
    $url_b = $_W['siteroot'].'addons/zh_cjdianc/img/'.$url;
    pdo_update('cjdc_store', array('qrcode' => $url_b), array('id' => $storeid));
    $qrcode = $url_b;
}
//var_dump($img);die;
//$img=getCoade($storeid);


//    var_dump($img);die;
include $this->template('web/index');